package com.wondertek.onvif.controller;

import com.wondertek.onvif.common.ResultBean;
import com.wondertek.onvif.service.OnvifDeviceService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 事件管理控制器
 * 处理前端事件管理页面的API请求
 */
@Slf4j
@RestController
@RequestMapping("/api")
@RequiredArgsConstructor
@CrossOrigin(origins = "*")
public class EventController {

    // 手动添加log变量以解决Lombok编译问题
    private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(EventController.class);

    private final OnvifDeviceService deviceService;

    /**
     * 获取所有事件列表
     * 
     * @param page 页码
     * @param size 页大小
     * @param deviceId 设备ID（可选）
     * @param eventType 事件类型（可选）
     * @param level 事件级别（可选）
     * @param startTime 开始时间（可选）
     * @param endTime 结束时间（可选）
     * @return 事件列表
     */
    @GetMapping("/events")
    public ResultBean<Map<String, Object>> getEvents(
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "10") int size,
            @RequestParam(required = false) Long deviceId,
            @RequestParam(required = false) String eventType,
            @RequestParam(required = false) String level,
            @RequestParam(required = false) String startTime,
            @RequestParam(required = false) String endTime) {
        try {
            // 如果指定了设备ID，获取该设备的所有通道事件
            if (deviceId != null) {
                Map<String, Object> events = deviceService.getDeviceEvents(deviceId, page, size, eventType, level, startTime, endTime);
                return ResultBean.success(events, "获取设备事件列表成功");
            } else {
                // 获取所有设备的事件
                Map<String, Object> events = deviceService.getAllEvents(page, size, eventType, level, startTime, endTime);
                return ResultBean.success(events, "获取事件列表成功");
            }
        } catch (Exception e) {
            log.error("获取事件列表失败", e);
            return ResultBean.error("获取事件列表失败: " + e.getMessage());
        }
    }

    /**
     * 订阅设备事件
     * 
     * @param deviceId 设备ID
     * @param eventConfig 事件配置
     * @return 订阅结果
     */
    @PostMapping("/devices/{deviceId}/events/subscribe")
    public ResultBean<Map<String, Object>> subscribeDeviceEvents(
            @PathVariable Long deviceId, 
            @RequestBody Map<String, Object> eventConfig) {
        try {
            Map<String, Object> result = deviceService.subscribeDeviceEvents(deviceId, eventConfig);
            return ResultBean.success(result, "订阅设备事件成功");
        } catch (Exception e) {
            log.error("订阅设备事件失败", e);
            return ResultBean.error("订阅设备事件失败: " + e.getMessage());
        }
    }

    /**
     * 取消订阅设备事件
     * 
     * @param deviceId 设备ID
     * @param subscriptionId 订阅ID
     * @return 取消订阅结果
     */
    @PostMapping("/devices/{deviceId}/events/unsubscribe/{subscriptionId}")
    public ResultBean<Map<String, Object>> unsubscribeDeviceEvent(
            @PathVariable Long deviceId, 
            @PathVariable String subscriptionId) {
        try {
            Map<String, Object> result = deviceService.unsubscribeDeviceEvent(deviceId, subscriptionId);
            return ResultBean.success(result, "取消订阅成功");
        } catch (Exception e) {
            log.error("取消订阅失败", e);
            return ResultBean.error("取消订阅失败: " + e.getMessage());
        }
    }

    /**
     * 获取设备事件订阅列表
     * 
     * @param deviceId 设备ID
     * @return 订阅列表
     */
    @GetMapping("/devices/{deviceId}/events/subscriptions")
    public ResultBean<List<Map<String, Object>>> getEventSubscriptions(@PathVariable Long deviceId) {
        try {
            List<Map<String, Object>> subscriptions = deviceService.getDeviceEventSubscriptions(deviceId);
            return ResultBean.success(subscriptions, "获取订阅列表成功");
        } catch (Exception e) {
            log.error("获取订阅列表失败", e);
            return ResultBean.error("获取订阅列表失败: " + e.getMessage());
        }
    }

    /**
     * 确认事件
     * 
     * @param eventId 事件ID
     * @return 确认结果
     */
    @PostMapping("/events/{eventId}/acknowledge")
    public ResultBean<Map<String, Object>> acknowledgeEvent(@PathVariable String eventId) {
        try {
            Map<String, Object> result = deviceService.acknowledgeEvent(eventId);
            return ResultBean.success(result, "事件确认成功");
        } catch (Exception e) {
            log.error("事件确认失败", e);
            return ResultBean.error("事件确认失败: " + e.getMessage());
        }
    }

    /**
     * 删除事件
     * 
     * @param eventId 事件ID
     * @return 删除结果
     */
    @DeleteMapping("/events/{eventId}")
    public ResultBean<Map<String, Object>> deleteEvent(@PathVariable String eventId) {
        try {
            Map<String, Object> result = deviceService.deleteEvent(eventId);
            return ResultBean.success(result, "删除事件成功");
        } catch (Exception e) {
            log.error("删除事件失败", e);
            return ResultBean.error("删除事件失败: " + e.getMessage());
        }
    }

    /**
     * 设置设备事件配置
     * 
     * @param deviceId 设备ID
     * @param config 事件配置
     * @return 设置结果
     */
    @PostMapping("/devices/{deviceId}/events/config")
    public ResultBean<Map<String, Object>> setEventConfig(
            @PathVariable Long deviceId, 
            @RequestBody Map<String, Object> config) {
        try {
            Map<String, Object> result = deviceService.setDeviceEventConfig(deviceId, config);
            return ResultBean.success(result, "设置事件配置成功");
        } catch (Exception e) {
            log.error("设置事件配置失败", e);
            return ResultBean.error("设置事件配置失败: " + e.getMessage());
        }
    }

    /**
     * 获取设备事件配置
     * 
     * @param deviceId 设备ID
     * @return 事件配置
     */
    @GetMapping("/devices/{deviceId}/events/config")
    public ResultBean<Map<String, Object>> getEventConfig(@PathVariable Long deviceId) {
        try {
            Map<String, Object> config = deviceService.getDeviceEventConfig(deviceId);
            return ResultBean.success(config, "获取事件配置成功");
        } catch (Exception e) {
            log.error("获取事件配置失败", e);
            return ResultBean.error("获取事件配置失败: " + e.getMessage());
        }
    }
}